package com.awan.ui;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.awan.adapter.LiveTVMenuAdapter;
import com.awan.adapter.MultimediaMenuAdapter;
import com.awan.utils.Constanta;
import com.awan.utils.HttpUtilGet;
import com.awan.utils.HttpUtilGet.ServerConnectinoException;
import com.beem.project.beem.R;
import com.dheina.lazylist.ImageLoader;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;

public class StreamingLiveList extends Activity{
	
	String TAG = getClass().getSimpleName();
	
	private LinearLayout tvstreamingContainer, radioStreamingContainer;
	private ProgressBar tvProgressBar, radioProgressBar;
	public ImageLoader imageLoader;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.streaming_list);
		imageLoader = new ImageLoader(this);
		
		tvstreamingContainer = (LinearLayout)findViewById(R.id.tvstreamingcontainer);
		radioStreamingContainer = (LinearLayout)findViewById(R.id.radiostreamingcontainer);
		
		tvstreamingContainer.setPadding(4, 4, 4, 4);
		radioStreamingContainer.setPadding(4, 4, 4, 4);
		
		tvProgressBar = (ProgressBar)findViewById(R.id.progressBarTV);
		radioProgressBar = (ProgressBar)findViewById(R.id.progressBarRadio);
		getTvList();
		
		
		/*ListView livestreamingtvlist = (ListView)findViewById(R.id.streamingtv_listview);
		livestreamingtvlist.setAdapter(new LiveTVMenuAdapter(this, this));
		livestreamingtvlist.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
					long arg3) {
				// TODO Auto-generated method stub
				String finalStreamUrl = getStreamUrl(LiveTVMenuAdapter.tvStreamUrl[pos]);
				Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(finalStreamUrl));
				startActivity(browserIntent);
			}
		});*/
	}
	/*private String getStreamUrl(String url){
		String finalUrl = url;
		if(url.trim().contains("linktone")){
			HttpUtilGet userService = HttpUtilGet.instance();
			try {
				String liveTvStreamingResponse = userService.getData(url);
				JSONObject jo=new JSONObject(liveTvStreamingResponse);
				finalUrl = jo.getString("url").trim();				
			} catch (ServerConnectinoException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return finalUrl;
	}*/
	private void getRadioList(){
		
		new Thread(){
			
			public void run(){
				
				HttpUtilGet userService = HttpUtilGet.instance();
				
				try {
					
					String liveRadioStreamingResponse = userService.getData(Constanta.radioUrlList);
					JSONArray radios = new JSONArray(liveRadioStreamingResponse);
					
					for(int a=0; a<radios.length(); a++){
						
						JSONObject jObj = radios.getJSONObject(a);
						
						String title = jObj.getString("title");
						final String url = jObj.getString("url");
						String thumb = jObj.getString("thumbnail");
						
						final ImageView radioIcon = new ImageView(StreamingLiveList.this);
						radioIcon.setImageResource(R.drawable.no_avatar);
						radioIcon.setOnClickListener(new OnClickListener() {							
							@Override
							public void onClick(View v) {
								// TODO Auto-generated method stub
								String finalStreamUrl = url;
								Intent browserIntent = new Intent(Intent.ACTION_VIEW);
								browserIntent.setDataAndType(Uri.parse(finalStreamUrl), "audio/*"); 
								startActivity(browserIntent);
							}
						});
						imageLoader.DisplayImage(thumb, radioIcon, 150, 150);
						final int x=a;
						runOnUiThread(new Runnable() {							
							@Override
							public void run() {
								// TODO Auto-generated method stub
								radioStreamingContainer.addView(radioIcon, x);
							}
						});
						
					}
					
				} catch (ServerConnectinoException e) {
					// TODO Auto-generated catch block
					Log.e(TAG, "Radio ServerConnectinoException:"+e.getMessage());
					e.printStackTrace();
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					Log.e(TAG, "Radio JSONException:"+e.getMessage());
					e.printStackTrace();
				}finally{
					runOnUiThread(new Runnable() {						
						@Override
						public void run() {
							// TODO Auto-generated method stub
							radioProgressBar.setVisibility(View.GONE);
							
						}
					});
					
					this.interrupt();
				}
			}
		}.start();
	}
	private void getTvList(){
		
		new Thread(){
			
			public void run(){
				
				HttpUtilGet userService = HttpUtilGet.instance();
				
				try {
					
					String liveRadioStreamingResponse = userService.getData(Constanta.tvUrlList);
					JSONArray radios = new JSONArray(liveRadioStreamingResponse);
					
					for(int a=0; a<radios.length(); a++){
						
						JSONObject jObj = radios.getJSONObject(a);
						
						String title = jObj.getString("title");
						final String url = jObj.getString("url");
						String thumb = jObj.getString("thumbnail");
						
						final ImageView tvIcon = new ImageView(StreamingLiveList.this);
						tvIcon.setImageResource(R.drawable.no_avatar);
						tvIcon.setOnClickListener(new OnClickListener() {							
							@Override
							public void onClick(View v) {
								// TODO Auto-generated method stub
								String finalStreamUrl = url;
								Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(finalStreamUrl));
								startActivity(browserIntent);
							}
						});
						imageLoader.DisplayImage(thumb, tvIcon, 150, 150);
						final int x=a;
						runOnUiThread(new Runnable() {							
							@Override
							public void run() {
								// TODO Auto-generated method stub
								tvstreamingContainer.addView(tvIcon, x);
							}
						});
						
					}
					
				} catch (ServerConnectinoException e) {
					// TODO Auto-generated catch block
					Log.e(TAG, "TV ServerConnectinoException:"+e.getMessage());
					e.printStackTrace();
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					Log.e(TAG, "TV JSONException:"+e.getMessage());
					e.printStackTrace();
				}finally{
					runOnUiThread(new Runnable() {						
						@Override
						public void run() {
							// TODO Auto-generated method stub
							tvProgressBar.setVisibility(View.GONE);
							getRadioList();
						}
					});
					this.interrupt();
				}
			}
		}.start();
	}
}
